import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class SocketServer {
  public static void main(String[] args) throws Exception {
    ServerSocket m_ServerSocket = new ServerSocket(1818);
 Socket clientSocket = null;
    int id = 0;
    while (true) {
 try {
      clientSocket = m_ServerSocket.accept();
   System.err.println("Have accepted new socket.");

 }
 catch (IOException e)
 {
  e.printStackTrace(System.err);
 }
      ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
      cliThread.start();
   
  
   try {
   System.err.println("Closing socket.");
   clientSocket.close();
  }
  catch (Exception e){
   System.err.println("Exception while closing socket.");
   e.printStackTrace(System.err);
  }

  System.err.println("Finished with socket, waiting for next connection.");
    }
  }
}

class ClientServiceThread extends Thread {
  Socket sock;
  int clientID = -1;
  boolean running = true;
  writeToFileClass wf = new writeToFileClass();

  ClientServiceThread(Socket s, int i) {
    sock = s;
    clientID = i;
  }

  @Override
  public void run() {
    //System.out.println("Accepted Client : ID - " + clientID + " : Address - "+ clientSocket.getInetAddress().getHostName());
    try {
  InputStream sockInput = sock.getInputStream();
        OutputStream sockOutput = sock.getOutputStream();
      while (running)
   {
  
            byte[] buf=new byte[1024];
            int bytes_read = 0;
            try {
                // This call to read() will wait forever, until the
                // program on the other side either sends some data,
                // or closes the socket.
                bytes_read = sockInput.read(buf, 0, buf.length);

                // If the socket is closed, sockInput.read() will return -1.
                if(bytes_read < 0) {
                    System.err.println("Tried to read from socket, read() returned < 0,  Closing socket.");
                    return;
                }
                /*System.err.println("Received "+bytes_read
                                   +" bytes, sending them back to client, data="
                                   +(new String(buf, 0, bytes_read)));*/
                String strTxt = new String(buf, 0, bytes_read);
                
                if(strTxt.indexOf("|PPD") > -1)
                {                  
                  wf.readData(strTxt);
                }
                
                sockOutput.write(buf, 0, bytes_read);
                // This call to flush() is optional - we're saying go
                // ahead and send the data now instead of buffering
                // it.
                sockOutput.flush();
            }
            catch (Exception e){
                System.err.println("Exception reading from/writing to socket, e="+e);
                e.printStackTrace(System.err);
                return;
            }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}